#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=2000000, inf=0x7fffffff;

long long min(long long a, long long b) {
  return a<b ? a : b;
}

int max(int a, int b) {
  return a>b ? a : b;
}

class Graph {
public:
  int n, m, ind[maxn+1], to[2*maxn-1], link[2*maxn-1], val[2*maxn-1], id[2*maxn-1], idv[maxn+1], ids[maxn+1][2];
  long long f[maxn+1];

  void addSide(int u, int v, int w, int id) {
    m++;
    to[m] = v;
    val[m] = w;
    this->id[m] = id;
    idv[id] = w;
    ids[id][0] = u;
    ids[id][1] = v;
    link[m] = ind[u];
    ind[u] = m;
  }

  long long calc(int o, int fa, int x, int y, int banid) {
    f[o] = 0;
    int maxa=0, maxb=0;
    for (int i=ind[o]; i; i=link[i]) {
      if (to[i]!=fa && id[i]!=banid) {
	calc(to[i], o, 0, 0, banid);
	f[o] = max(f[o], f[to[i]]+val[i]);
	if (f[to[i]]+val[i]>maxa) {
	  maxb=maxa;
	  maxa=f[to[i]]+val[i];
	} else if (f[to[i]]+val[i]>maxb) {
	  maxb=f[to[i]]+val[i];
	}
      }
    }
    if (x) {
      calc(x, o, 0, 0, banid);
      f[o] = max(f[o], f[x]+y);
      if (f[x]+y>maxa) {
	maxb=maxa;
	maxa=f[x]+y;
      } else if (f[x]+y>maxb) {
	maxb=f[x]+y;
      }
    }
    return maxa+maxb;
  }
};

unsigned long long get(unsigned long long &num){
  num^=(num<<13);
  num^=(num>>17);
  num^=(num<<5);
  return num;
}

void gen(Graph &g){
  int n,B,D;
  unsigned long long num;
  scanf("%d%llu%d%d",&n,&num,&B,&D);
  g.n = n;
  int a, b;
  for(int i=2;i<=n;i++){
    a=get(num)%min(i-1,B)+i-min(i-1,B);
    b=get(num)%D;
    g.addSide(i, a, b, i-1);
    g.addSide(a, i, b, i-1);
  }
}

int getFa(int s[], int o) {
  return s[o]==o ? o : (s[o]=getFa(s, s[o]));
}

int main() {
  freopen("path.in", "r", stdin);
  freopen("path.out", "w", stdout);

  static Graph g;
  gen(g);

  long long ans=0;
  static int set[maxn+1];
  for (int i=1; i<g.n; i++) {
    long long minv=inf;
    for (int j=1; j<=g.n; j++) {
      set[j] = j;
    }
    for (int j=1; j<g.n; j++) {
      if (j!=i) {
	set[getFa(set, g.ids[j][1])] = set[getFa(set, g.ids[j][0])];
      }
    }
    for (int j=1; j<=g.n; j++) {
      for (int k=j+1; k<=g.n; k++) {
	if (getFa(set, j)!=getFa(set, k)) {
	  minv = min(minv, g.calc(j, 0, k, g.idv[i], i));
	}
      }
    }
    ans ^= i*minv%998244353;
  }

  printf("%lld", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
